package com.triptogether.shoppinglist.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.triptogether.R;
import com.triptogether.baseclasses.BaseFragment;
import com.triptogether.shoppinglist.activities.OnePaneProductsActivity;
import com.triptogether.shoppinglist.fragments.ShoppingListsFragment.OnShoppingListSelectedListener;
import com.triptogether.trip.fragments.TripMainFragment;
import com.triptogether.utils.db.models.ShoppingList;

public class ShoppingListMainFragment extends BaseFragment implements OnShoppingListSelectedListener {
	
	FragmentManager fragmentManager;
	boolean isDualPane;
	ShoppingList currentShoppingList;
	Long currentTripId;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    return inflater.inflate(R.layout.fragment_shopping_lists_main, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState); 
	    
	    // Check if this is DualPane
	    isDualPane = isDualPane();
	    
	    // Get and save the fragment manager for future use
	    fragmentManager = getActivity().getSupportFragmentManager();
		
		// If the savedInstanceState is null, take the trip id from the arguments that was sent with the fragment
		if (savedInstanceState == null) {
			currentTripId = getArguments().getLong(TripMainFragment.ARG_TRIP_ID);
		} else {
			// Else, take the trip id from the savedInstanceBundle, that was saved before
			currentTripId = savedInstanceState.getLong(TripMainFragment.ARG_TRIP_ID);
		}
	    
		// Initialize the screen in the correct configuration
		if (isDualPane) {
			initDualPane(savedInstanceState);
		} else {
			initOnePane(savedInstanceState);
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
	    super.onSaveInstanceState(outState);
	    
	    // Save the current Trip id
	    outState.putLong(TripMainFragment.ARG_TRIP_ID, currentTripId);
	    
	    // Save the chosen shopping list, if any
		if (currentShoppingList != null) {
			outState.putLong(ProductsListFragment.ARG_SHOPPING_LIST_ID,
			        currentShoppingList.getId());
		}
	}

	private void initOnePane(Bundle savedInstanceState) {
		ShoppingListsFragment shoppingListsFragment;
		
		if(savedInstanceState == null) {
            // Initialize the ShoppingList fragment for the first time
    		shoppingListsFragment = new ShoppingListsFragment();
    		
    		// Add the trip id argument for this fragment
    		Bundle arguments = new Bundle();
    		arguments.putLong(TripMainFragment.ARG_TRIP_ID, currentTripId);
    		shoppingListsFragment.setArguments(arguments);
        	
        	// Add the ShoppingListFragment to the container of this layout
    		fragmentManager.beginTransaction().replace(R.id.framelayout_shopping_list_container, shoppingListsFragment).commit();
		} else {
			// Only find the ShoppingListFragment on the activity
			shoppingListsFragment = (ShoppingListsFragment) fragmentManager.findFragmentById(R.id.framelayout_shopping_list_container);
		}

    	shoppingListsFragment.setHasOptionsMenu(true);
    	shoppingListsFragment.setOnShoppingListSelectedListener(this);
    }

	private void initDualPane(Bundle savedInstanceState) {
		// Init the ShoppingList fragment
		ShoppingListsFragment shoppingListsFragment = (ShoppingListsFragment) fragmentManager.findFragmentById(R.id.fragment_shopping_list);
		shoppingListsFragment.setHasOptionsMenu(true);
		shoppingListsFragment.setOnShoppingListSelectedListener(this);
		
		// Add the trip id argument for this fragment
		Bundle arguments = new Bundle();
		arguments.putLong(TripMainFragment.ARG_TRIP_ID, currentTripId);
		shoppingListsFragment.setArguments(arguments);
		
		// Check if a shoppingList already has been chosen for case this activity created again (configuration changes)
		if (savedInstanceState != null &&
		    savedInstanceState.getLong(ProductsListFragment.ARG_SHOPPING_LIST_ID,
		            Long.MIN_VALUE) != Long.MIN_VALUE) {
			long chosenShoppingListId = savedInstanceState.getLong(ProductsListFragment.ARG_SHOPPING_LIST_ID);
			
			// Initiate the ProductsListFragment and show it in its frame
			ProductsListFragment initiatedProductsListFragment = initProductListFragment(chosenShoppingListId);
			fragmentManager.beginTransaction().replace(R.id.framelayout_products,
			        initiatedProductsListFragment).commit();	
		}
    }

	public static ProductsListFragment initProductListFragment(long shoppingListId) {
	    // Add extra to the ProductsList fragment
		Bundle bundle = new Bundle();
		bundle.putLong(ProductsListFragment.ARG_SHOPPING_LIST_ID, shoppingListId);
		
		// Init the new ProductsListFragment with the ShoppingListId argument
		ProductsListFragment productsListFragment = new ProductsListFragment();
		productsListFragment.setHasOptionsMenu(true);
		productsListFragment.setArguments(bundle);
		
		return productsListFragment;
    }

	@Override
    public void ShoppingListSelected(ShoppingList selectedShoppingList) {
		// Get the selected shoppingList from the ShoppingListFragment
		currentShoppingList = selectedShoppingList;

		if (isDualPane) {
			// Init and get the ProductListFragment of the currentShoppingList
			ProductsListFragment initiatedProductsListFragment = initProductListFragment(currentShoppingList.getId());
			
			fragmentManager.beginTransaction().replace(R.id.framelayout_products,
			        initiatedProductsListFragment).commit();	
		} else {
			Intent onePaneProductsActivityIntent = new Intent(getActivity(), OnePaneProductsActivity.class);
			onePaneProductsActivityIntent.putExtra(ProductsListFragment.ARG_SHOPPING_LIST_ID, currentShoppingList.getId());
			onePaneProductsActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(onePaneProductsActivityIntent);
		}
    }
	
	public boolean isDualPane() {
		return getView().findViewById(R.id.framelayout_shopping_list_container) == null;
	}
	
	public void selectShoppingListsByTripId(long tripId) {
		currentTripId = tripId;
		currentShoppingList = null;
		
		if(isDualPane) {
			initDualPane(null);
		} else {
			initOnePane(null);
		}
	}
}
